<?php

namespace App\Http\Controllers;

use App\Assorts;
use App\Boxs;
use App\Goods;
use App\Tags;
use Faker\Provider\Uuid;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;

use App\Dics;

class AssortController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $goods=Goods::where('number','>','0')->groupBy(['symbol'])->get();

        return view('system.assort.index',compact('goods'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    public function ajaxnumber(Request $request)
    {
        $this->validate($request, [
            'serial' => 'required|max:255',
            'size' => 'required|max:255',
            'color' => 'required',
        ]);

        $good = Goods::where('serial',$request->get('serial'))->where('color',$request->get('color'))->where('size',$request->get('size'))->first();
        if(empty($good)) {
            return response()->json(["error" => "没有数据"]);
        }else{
            return response()->json(["id" => $good->id, "serial" => $good->serial, "category" => $good->category->key, "size" => $good->size->val, "color" => $good->color->key, "number" => $good->number]);
        }
    }

    public function ajaxchangestate(Request $request)
    {
        $this->validate($request, [
            'orderno' => 'required|max:255',
            'state' => 'required|max:255',
        ]);

        $box = Boxs::where('orderno',$request->get('orderno'))->where('state',$request->get('state'))->first();

        if(!empty($box)){
            $box->state =$box->state->val+1;
            $box->save();
            return response()->json(['orderno'=>$box->orderno,'state'=>$box->state]);
        }else{
            return response()->json(['error'=>"当前状态无法切换"]);
        }
    }

    public function checkstore(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'wechat' => 'required|max:255',
            'mobile' => 'required',
            'address' => 'required|max:255',
            'assortJson' => 'required',

        ]);

        $input = $request->all();

        $assortJson = $input["assortJson"];
        $assorts = \GuzzleHttp\json_decode($assortJson);

        $assorts->ispost=true;

        //上面已经取到前端提交的订单信息，接下来应该去比对数据的实际数量。
        foreach ($assorts->goodsitem as $item) {

            $category = Dics::where('key',$item->category)->first();
            $size = Dics::where('key',$item->size)->first();
            $color = Dics::where('key',$item->color)->first();

            $good = Goods::where('serial',$item->serial)->where(['category'=>$category->val,'size'=>$size->val,'color'=>$color->val])->where('number','>=',$item->number)->first();

            if($good != null){
                $item->serial=$good->serial;
                $item->price = $good->price;
                $item->id=$good->id;
            }else{
                if($assorts->ispost){
                    $assorts->ispost=false;
                }
                $item->number="0";
            }
        }

        return view('system.assort.check',compact('assorts'));
    }

    public function showboxs($keyworld='')
    {
        if(empty($keyworld)) {
            $boxs = Boxs::where('creator', Auth::user()->id)->orderBy('updated_at', 'desc')->paginate(15);
        }else{
            $boxs = Boxs::where('creator',Auth::user()->id)->where(function($query) use ($keyworld){
                $query->whereRaw('DATE_FORMAT(created_at,\'%Y%m%d%h%i%s\')=\'' . $keyworld . '\'')
                    ->orWhere(function($query) use ($keyworld){
                        $query->where('express_out_no',$keyworld);
                    });
            })->paginate(15);
            //$boxs = Boxs::where('creator', Auth::user()->id)->Where('express_out_no', $keyworld)->orwhereRaw('DATE_FORMAT(created_at,\'%Y%m%d%h%i%s\')=\'' . $keyworld . '\'')->paginate(15);
        }

        $tags = Tags::all();

        return view('system.assort.myboxs',compact(['boxs','tags']));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //json_decode($request->assortJson)
        $this->validate($request, [
            'assortJson' => 'required',
            'name' => 'required|max:255',
            'wechat' => 'required|max:255',
            'mobile' => 'required',
            'address' => 'required',
            'express_out_company' => 'required',
            'express_out_no' => 'required',
            'express_in_company' => 'required',
            'express_in_no' => 'required',
        ]);

        $message ="未知性错误"; //用于记录操作返回结果提示
        $input = $request->all();
        $boxs = $input;
        $boxs["orderno"] = Uuid::uuid();
        $boxs["creator"] = Auth::user()->id;
        $boxs["state"] = "1";
        $boxs["history"] = $input["assortJson"];

        $createBoxs = Boxs::create($boxs);

        if($createBoxs){

            $assrotJson = json_decode($input["assortJson"]);

            foreach ($assrotJson as $assort){

                $createAssort= Assorts::create(["goods_id"=>$assort->id,
                    "serial"=>$assort->serial,
                    "category"=>$assort->category,
                    "size"=>$assort->size,
                    "color"=>$assort->color,
                    "state"=>"1",
                    "number"=>$assort->number,
                    "price"=>GoodsController::calcPrice($assort->price),
                    "discount"=>GoodsController::calcDiscount($assort->price,$assort->serial),
                    "orderno"=>$boxs["orderno"],
                    "img"=>pathinfo($assort->img)["basename"]]);

                if($createAssort){

                    $good=Goods::where("id",$assort->id)->first();

                    if($good!=null) {
                        $good->number = $good->number - $assort->number;
                        $good->save();
                        $message = "盒子已经成功的搭配";
                    }else{
                        Boxs::where("orderno",$boxs["orderno"])->delete();
                        $message ="物品库存数量不足";
                        break;
                    }

                }else{

                    Boxs::where("orderno",$boxs["orderno"])->delete();
                    $createBoxs=false;
                    break;
                }
            }
        }

        return Redirect::to(url('system/assort/sublimt'))->with('message', $message);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function print($orderno)
    {
        $boxs = Boxs::where('orderno',$orderno)->first();

        return view('system.assort.print',compact('boxs'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
